<?php
// 发布留言
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");
include_once("u_getLoginInfo.php");
if($status != 1){
    die("<script>alert('账户处于待注销状态，不可操作！');history.go(-1);</script>");
}
$leaveMsg = $_POST['leaveMsg'];

function generate_random_string($length) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $random_string = '';

    for ($i = 0; $i < $length; $i++) {
        $random_string .= $characters[rand(0, strlen($characters) - 1)];
    }

    return $random_string;
}

$msgID = generate_random_string(10); // 生成一个长度为 10 的随机字ID

// 设置时区为中国
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间
$now_Date = date('Y-m-d H:i:s');

$select = 
"SELECT *
 FROM msg_infos
 WHERE DATE_FORMAT(msg_date, '%Y-%m-%d') = '". date('Y-m-d')."'";

$result = mysqli_query($connect_DB, $select);

if(mysqli_num_rows($result) >= 5){
    echo "<script>alert('当天留言数量已达到上限！');location='user_index.php';</script>";
    exit;
}

$sql = 
"INSERT INTO msg_infos (msg_ID, user_ID, msg_date, msg_content) 
VALUES ('".$msgID."', '".$userID."', '".$now_Date."', '".$leaveMsg."')";

if(mysqli_query($connect_DB, $sql)){
    echo "<script>alert('发布留言成功！');location='user_index.php';</script>";
}else{
    die("<script>alert('发布留言失败！请重试！');location='user_index.php';</script>");
}

$connect_DB->close();
?>